 //
//  AppDelegate.swift
//  weibo
//
//  Created by iobit on 09/05/2017.
//  Copyright © 2017 zhuxu. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
 
 
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        

        

        
        
        setupAddtion()
        //实例化window
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        //设置根控制器
        window?.rootViewController = WBMainViewController()
        //让window可见
        window?.makeKeyAndVisible()
        
        loadAppInfo()

        return true
    }

}

 
// MARK: - 设置应用额外信息
extension AppDelegate {
    
    func setupAddtion() {
        //1\设置最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        //2\设置顶部左上角转圈网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3\ 获取授权显示通知，上方提示条的sound/alert/badge/carPlay
        if #available(iOS 10.0, *) {
            
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound, .carPlay]) { (isSuccess, error) in
                print("授权"+(isSuccess ? "成功" : "失败"))
            }
        } else {
            // ios10.0以下授权方式
            let userNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(userNotificationSettings)
        }
    }
    
}
 
 
// 从服务器加载应用程序信息
extension AppDelegate {

    func loadAppInfo() {
    
        //1、模拟异步
        DispatchQueue.global().async {
            //1、url
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            //2、url
            let data = NSData(contentsOf: url!)
            //3、写入磁盘
            let dicDir = NSHomeDirectory()
            let jsonPath = dicDir + "/Documents/main.json"
            data?.write(toFile: jsonPath, atomically: true)
            print("应用程序加载完毕\(jsonPath)")
        }
        
    }
    
}
